from flask_restful import Resource


from model.crud import UpdateInstance, QueryOneInstance, DeleteInstance
from model.tables import Follow
from restful.data import DataUid
from restful.response import ResponseConflict, ResponseSuccess
from restful.session import current_user_required, data


class FollowOrCancel(Resource):
    @current_user_required
    @data(DataUid())
    def get(self,data,current_user):
        follow = QueryOneInstance(Follow,{'uid':current_user.id,'uid1':data['uid']})
        return ResponseSuccess(data={
            'followed':bool(follow)
        })

    @current_user_required
    @data(DataUid())
    def post(self,data,current_user):
        follow = QueryOneInstance(Follow,{'uid':current_user.id,'uid1':data['uid']})

        if follow:
            return ResponseConflict("请勿重复关注")

        UpdateInstance(Follow(uid=current_user.id,uid1=data['uid']))
        return ResponseSuccess("已关注")

    @current_user_required
    @data(DataUid())
    def delete(self,data,current_user):
        follow = QueryOneInstance(Follow,{'uid':current_user.id,'uid1':data['uid']})

        if not follow:
            return ResponseConflict("关注不存在")
        DeleteInstance(follow)
        return ResponseSuccess("已取消关注")

